package cn.stylefeng.guns.service.impl;

import cn.stylefeng.guns.dao.ISaleStockService;
import cn.stylefeng.guns.dao.entity.SaleStock;
import cn.stylefeng.guns.dao.entity.neww.PwkCg;
import cn.stylefeng.guns.dao.entity.neww.PwkMdXs;
import cn.stylefeng.guns.dao.entity.neww.PwkXs;
import cn.stylefeng.guns.dao.service.neww.IPwkCgService;
import cn.stylefeng.guns.dao.service.neww.IPwkKcService;
import cn.stylefeng.guns.dao.service.neww.IPwkMdXsService;
import cn.stylefeng.guns.dao.service.neww.IPwkXsService;
import cn.stylefeng.guns.util.ConfigUtil;
import cn.stylefeng.guns.util.RebateDateUtil;
import cn.stylefeng.roses.kernel.email.api.MailSenderApi;
import cn.stylefeng.roses.kernel.email.api.pojo.SendMailParam;
import cn.stylefeng.roses.kernel.email.jdk.JavaMailSender;
import cn.stylefeng.roses.kernel.timer.api.TimerAction;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 月报任务
 */
@Component
@Slf4j
public class MailTimer implements TimerAction {

    public static MailSenderApi mailSenderApi = new JavaMailSender();

    @Resource
    private IPwkCgService pwkCgService;
    @Resource
    private IPwkKcService pwkKcService;
    @Resource
    private IPwkMdXsService pwkMdXsService;
    @Resource
    private IPwkXsService pwkXsService;
    @Resource
    private ISaleStockService saleStockService;


    @Override
    public void action(String params) {
        Long startTime = RebateDateUtil.getLastDayBegin(1);
        Long endTime = RebateDateUtil.getLastDayEnd(1);
        //采购
        Long cgCount = pwkCgService.count(Wrappers.<PwkCg>lambdaQuery()
                .ge(PwkCg::get时间, new Date(startTime)).le(PwkCg::get时间, new Date(endTime))
        );
        //门店销售
        Long mdXsCount = pwkMdXsService.count(Wrappers.<PwkMdXs>lambdaQuery()
                .ge(PwkMdXs::get销售时间, new Date(startTime))
                .le(PwkMdXs::get销售时间, new Date(endTime))
        );
        //销售
        Long xsCount = pwkXsService.count(Wrappers.<PwkXs>lambdaQuery()
                .ge(PwkXs::get时间, new Date(startTime)).le(PwkXs::get时间, new Date(endTime))
        );

        long count = saleStockService.count(Wrappers.<SaleStock>lambdaQuery()
                .ge(SaleStock::getSaleTime, new Date(startTime)).le(SaleStock::getSaleTime, new Date(endTime))
                .eq(SaleStock::getTest, 0)
                .eq(SaleStock::getType, "supplier")
        );


        try {
            if (cgCount * mdXsCount * xsCount == 0 || count != cgCount) {
                SendMailParam sendMailParam = new SendMailParam();
                List<String> mailTo = ConfigUtil.getMailTo();
                sendMailParam.setTos(mailTo);
                sendMailParam.setContent("采购数量：" + cgCount + "；门店销售数量：" + mdXsCount + "；销售数量：" + xsCount);
                sendMailParam.setTitle("数据告警");
                mailSenderApi.sendMail(sendMailParam);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("日报发送错误", e);
        }
    }
}
